/**
 * 
 */
var ACCESS_TOKEN = "CAACEdEose0cBALsgZAp3GhIKSzKYZAtHjzXgyjSBKM97SI2qQZA3TZC1SYgLtw9tVzT90Gg6c6aDgy2aYpktsttN1lsaZBdPslko1qLW2r9Qa44GUMLJGBHVX3MHqapb38tzL1E70xQDuCF2wRsCUZB5RxeW7N3Au6EJzMZCSdnrujShZCvEwLPVNzEeGqkX00CFW6lDHSULBKJ5Iy84nean";

// When ready, load the FB API.
$(document).ready(function() {
	
	window.fbAsyncInit = function() {
		FB.init({
			appId : '1566881833546846',
			xfbml : true,
			version : 'v2.2'
		});
		
		// Initialize the gallery
		initGalleriffic();
		
		// Get the Facebook pictures.
		FB.api(
			    "10152541115798401?fields=name,photos,likes&access_token="+ACCESS_TOKEN,
			    function (response) {
			      if (response && !response.error) {
			        /* handle the result */
			      }
			      printImages(response);
			    }
			);
	};

	(function(d, s, id) {
		var js, fjs = d.getElementsByTagName(s)[0];
		if (d.getElementById(id)) {
			return;
		}
		js = d.createElement(s);
		js.id = id;
		js.src = "//connect.facebook.net/en_US/sdk.js";
		fjs.parentNode.insertBefore(js, fjs);
	}(document, 'script', 'facebook-jssdk'));

	// Print all the images
	var printImages = function(albumInfo){
		if(!albumInfo.photos)
			return false;
		var photos = albumInfo.photos.data;
		
		for (var i=0; i < photos.length; i++) {
			gallery.insertImage('<li>																							\
					<a class="thumb" href="'+photos[i].picture+'" title="'+photos[i].name+'">\
						<img src="'+photos[i].source+'" alt="'+photos[i].name+'" />\
					</a>																										\
					<div class="caption">																						\
						<div class="download">																					\
							<a href="'+photos[i].source+'">Download Original</a>		\
						</div>																									\
						<div class="image-title">Dynamically Added Image</div>													\
						<div class="image-desc">																				\
							<img src="'+photos[i].source+'" alt="ringydingydo" />		\
							Photo taken by Brayan Zimmerli				\
						</div>																									\
					</div>																										\
				</li>', i);
			
			e.preventDefault();
		}
		
	};
	
	var initGalleriffic = function (){
		// We only want these styles applied when javascript is enabled
		$('div.navigation').css({'width' : '300px', 'float' : 'left'});
		$('div.content').css('display', 'block');

		// Initially set opacity on thumbs and add
		// additional styling for hover effect on thumbs
		var onMouseOutOpacity = 0.67;
		$('#thumbs ul.thumbs li').opacityrollover({
			mouseOutOpacity:   onMouseOutOpacity,
			mouseOverOpacity:  1.0,
			fadeSpeed:         'fast',
			exemptionSelector: '.selected'
		});

		// Enable toggling of the caption
		var captionOpacity = 0.0;
		$('#captionToggle a').click(function(e) {
			var link = $(this);
			
			var isOff = link.hasClass('off');
			var removeClass = isOff ? 'off' : 'on';
			var addClass = isOff ? 'on' : 'off';
			var linkText = isOff ? 'Hide Caption' : 'Show Caption';
			captionOpacity = isOff ? 0.7 : 0.0;

			link.removeClass(removeClass).addClass(addClass).text(linkText).attr('title', linkText);
			$('#caption span.image-caption').fadeTo(1000, captionOpacity);
			
			e.preventDefault();
		});
		
		// Initialize Advanced Galleriffic Gallery
		var gallery = $('#thumbs').galleriffic({
			delay:                     2500,
			numThumbs:                 20,
			preloadAhead:              0,
			enableTopPager:            true,
			enableBottomPager:         true,
			maxPagesToShow:            7,
			imageContainerSel:         '#slideshow',
			controlsContainerSel:      '#controls',
			captionContainerSel:       '#caption',
			loadingContainerSel:       '#loading',
			renderSSControls:          true,
			renderNavControls:         true,
			playLinkText:              'Play Slideshow',
			pauseLinkText:             'Pause Slideshow',
			prevLinkText:              '&lsaquo; Previous Photo',
			nextLinkText:              'Next Photo &rsaquo;',
			nextPageLinkText:          'Next &rsaquo;',
			prevPageLinkText:          '&lsaquo; Prev',
			enableHistory:             false,
			autoStart:                 false,
			syncTransitions:           true,
			defaultTransitionDuration: 900,
			onSlideChange:             function(prevIndex, nextIndex) {
				// 'this' refers to the gallery, which is an extension of $('#thumbs')
				this.find('ul.thumbs').children()
					.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
					.eq(nextIndex).fadeTo('fast', 1.0);
			},
			onTransitionOut:           function(slide, caption, isSync, callback) {
				slide.fadeTo(this.getDefaultTransitionDuration(isSync), 0.0, callback);
				caption.fadeTo(this.getDefaultTransitionDuration(isSync), 0.0);
			},
			onTransitionIn:            function(slide, caption, isSync) {
				var duration = this.getDefaultTransitionDuration(isSync);
				slide.fadeTo(duration, 1.0);
				
				// Position the caption at the bottom of the image and set its opacity
				var slideImage = slide.find('img');
				caption.width(slideImage.width())
					.css({
						'bottom' : Math.floor((slide.height() - slideImage.outerHeight()) / 2),
						'left' : Math.floor((slide.width() - slideImage.width()) / 2) + slideImage.outerWidth() - slideImage.width()
					})
					.fadeTo(duration, captionOpacity);
			},
			onPageTransitionOut:       function(callback) {
				this.fadeTo('fast', 0.0, callback);
			},
			onPageTransitionIn:        function() {
				this.fadeTo('fast', 1.0);
			},
			onImageAdded:              function(imageData, $li) {
				$li.opacityrollover({
					mouseOutOpacity:   onMouseOutOpacity,
					mouseOverOpacity:  1.0,
					fadeSpeed:         'fast',
					exemptionSelector: '.selected'
				});
			}
		});

	}
	
	
});
